package com.cloud.cmt.usermanage.controller;

import com.angela.cloud.common.base.ResultMessage;
import com.angela.cloud.common.constant.ResultCodeConstant;
import com.cloud.cmt.usermanage.base.BaseVaule;
import com.cloud.cmt.usermanage.base.PagePo;
import com.cloud.cmt.usermanage.base.PageRequestFactory;
import com.cloud.cmt.usermanage.entity.UserInfo;
import com.cloud.cmt.usermanage.entity.vo.UserInfoVO;
import com.cloud.cmt.usermanage.service.UserInfoRepository;
import com.cloud.cmt.usermanage.service.UserInfoVORepository;
import com.cloud.cmt.usermanage.service.impl.UserServiceImpl;
import com.google.common.base.Strings;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: wcy
 * Date: 2017/9/5
 * Time: 15:45
 */
@RestController
@RequestMapping("/user")
@Api(value = "/用户", description = "用户管理-用户信息增删查改等-by wcy")
public class UserCtl {

    @Autowired
    UserInfoRepository userInfoRepository;

    @Resource
    UserInfoVORepository userInfoVORepository;

    @Resource
    UserServiceImpl userService;

    @ApiOperation(value = "通过用户id获取信息")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ResultMessage<UserInfo> getUser(@PathVariable long id){
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        resultMessage.setData(userInfoRepository.findOne(id));
        return resultMessage;
    }

    @ApiOperation(value = "保存用户信息")
    @PostMapping(value = "")
    @ResponseBody
    public ResultMessage<UserInfo> save(@RequestBody UserInfo userInfo){
        ResultMessage resultMessage = new ResultMessage();
        if(Strings.isNullOrEmpty(userInfo.getUserName())){
            resultMessage.setCode(ResultCodeConstant.SERVICE_ERROR_CODE);
            resultMessage.setMessage("用户名不能为空");
        }else if(Strings.isNullOrEmpty(userInfo.getPassword())){
            resultMessage.setCode(ResultCodeConstant.SERVICE_ERROR_CODE);
            resultMessage.setMessage("密码不能为空");
        }else if(userInfo.getOrgId()!=null && userInfo.getOrgId()>1){
            resultMessage.setCode(ResultCodeConstant.SERVICE_ERROR_CODE);
            resultMessage.setMessage("组织机不能为空");
        }else{
            userInfo= userInfoRepository.save(userInfo);
            if (userInfo.getId() !=null){
                resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
                resultMessage.setId(userInfo.getId()+"");
            }else{
                resultMessage.setCode(ResultCodeConstant.SERVICE_ERROR_CODE);
            }
        }

        return resultMessage;
    }

    @ApiOperation(value = "更新用户信息")
    @PutMapping(value = "")
    @ResponseBody
    public ResultMessage<UserInfo> update(@RequestBody UserInfo userInfo){
        ResultMessage resultMessage = new ResultMessage();
        userInfoRepository.saveAndFlush(userInfo);

        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);

        return resultMessage;
    }

    @ApiOperation(value = "获取所有用户信息")
    @PostMapping(value = "/findPage")
    @ResponseBody
    public ResultMessage<List<UserInfoVO>> findPage(@RequestBody PagePo<BaseVaule> pagePo) {
        Page<UserInfoVO> userInfoList = userInfoVORepository.userInfo("%"+pagePo.getFields().getValue()+"%", PageRequestFactory.getPageInfo(pagePo));
        return ResultMessage.success(userInfoList);
    }

    @ApiOperation(value = "通过用户信息获取信息")
    @PostMapping(value = "/getUserInfo")
    @ResponseBody
    public List<UserInfo> getUserInfo(@RequestParam(value="name",required=false)  String name) {
        return userService.findByName(name);
    }

    @ApiOperation(value = "通过用户id删除信息")
    @DeleteMapping(value = "/{id}")
    public ResultMessage remove(@PathVariable("id") Long id) {
        ResultMessage resultMessage = new ResultMessage();
        userInfoRepository.delete(id);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        return  resultMessage;
    }

}
